package cn.herlioner.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.print.DocFlavor.STRING;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.mapper.ActionMapper;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.interceptor.SessionAware;
import org.junit.Test;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


import cn.herlioner.dao.impl.AdministratorDAOImpl;
import cn.herlioner.dao.impl.ObjectDAOImpl;
import cn.herlioner.dao.impl.PermissionsDAOImpl;
import cn.herlioner.dao.impl.StudentDAOImpl;
import cn.herlioner.dao.impl.TeacherDAOImpl;
import cn.herlioner.pojo.Administrator;
import cn.herlioner.pojo.Login;
import cn.herlioner.pojo.Permissions;
import cn.herlioner.pojo.Student;
import cn.herlioner.pojo.Teacher;
import cn.herlioner.pojo.TSObject;
import cn.herlioner.service.LoginServiceImpl;


public class LoginAction extends ActionSupport implements SessionAware {
	LoginServiceImpl loginServiceImpl = new LoginServiceImpl();
	/**
	 * @return
	 */

	public String login(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Long account = (long) Integer.parseInt(request.getParameter("account"));
		String pad = request.getParameter("password");
		Map session = ActionContext.getContext().getSession();
		int judgenum = loginServiceImpl.login(account, pad);
		System.out.println(judgenum);
		switch (judgenum) {
			case 0:
				String warring = "账号或密码错误，请重新登录！！";
				request.setAttribute("warring", warring);
				break;
			case 1:
				AdministratorDAOImpl adminstratorDAO = new AdministratorDAOImpl();
				Administrator administrator = adminstratorDAO.get((long) 1);
				session.put("adminstrator ",administrator);
				break;
			case 2:
				TeacherDAOImpl teacherDAOImpl = new TeacherDAOImpl();
				Teacher teacher = teacherDAOImpl.get((long) 2);
				session.put("teacher ",teacher);
				break;
			case 3:
				StudentDAOImpl studentDAOImpl = new StudentDAOImpl();
				Student student = studentDAOImpl.get((long) 3);
				session.put("student ",student );
				break;
		default:
			break;
		}
		PermissionsDAOImpl permissionsDAO = new PermissionsDAOImpl();
		//将judge的作为参数,得到该权限可以拥有的导航栏
		List<Permissions> permissionss = permissionsDAO.getListByPermissions(String.valueOf(judgenum));
		//先将首页元素添加到permissionssreturn列表
		List<Permissions> permissionsss = new ArrayList();
		permissionsss.add(permissionsDAO.getListByPermissions("0").get(0));
		//循环permissionss将其添加到permissionssreturn
		for (Permissions per : permissionss) {
			permissionsss.add(per);
		}
		session.put("Num",account);
		session.put("permissionssreturn", permissionsss);
		session.put("pageNum",1);
		System.out.println(judgenum);
		if(judgenum==0){
				String warring = "账号或密码错误，请重新登录！！";
				request.setAttribute("warring", warring);
				return "unsuccess";
		}else{
				return "success";
		}}


	@Override
	public void setSession(Map<String, java.lang.Object> arg0) {
		// TODO Auto-generated method stub
		
	}
		
}
